from fastapi import APIRouter
from fastapi import Depends, Request
from sqlalchemy.ext.asyncio import AsyncSession

from config.enums import BusinessType
from config.get_db import get_db
from module_admin.annotation.log_annotation import Log
from module_admin.aspect.interface_auth import CheckUserInterfaceAuth
from module_admin.entity.vo.user_vo import CurrentUserModel
from module_admin.service.login_service import LoginService
from module_basics.entity.vo.log_vo import ContrastLogPageQueryModel, DeleteContrastLogModel, ContrastLogModel
from module_basics.service.log_service import ContrastLogService
from module_basics.util.comparison_log_util import ComparisonLogUtil
from utils.common_util import bytes2file_response
from utils.log_util import logger
from utils.page_util import PageResponseModel
from utils.response_util import ResponseUtil

contrasLogController = APIRouter(prefix='/contras/log', dependencies=[Depends(LoginService.get_current_user)])


@contrasLogController.get(
    '/list',
    response_model=PageResponseModel,
    dependencies=[Depends(CheckUserInterfaceAuth('contras:log:list'))],
)
async def get_contras_log_list(
        contras_log_page_query: ContrastLogPageQueryModel = Depends(ContrastLogPageQueryModel.as_query),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取分页数据
    contras_log_page_query_result = await ContrastLogService.get_contrast_log_list_services(
        query_db, contras_log_page_query, is_page=True
    )
    logger.info('获取成功')

    return ResponseUtil.success(model_content=contras_log_page_query_result)


@contrasLogController.delete('/clean', dependencies=[Depends(CheckUserInterfaceAuth('contras:log:remove'))])
@Log(title='文档对比日志', business_type=BusinessType.CLEAN)
async def clear_contras_log(query_db: AsyncSession = Depends(get_db)):
    clear_contras_log_result = await ContrastLogService.clear_operation_log_services(query_db)
    logger.info(clear_contras_log_result.message)

    return ResponseUtil.success(msg=clear_contras_log_result.message)


@contrasLogController.delete('/delete/{ids}', dependencies=[Depends(CheckUserInterfaceAuth('contras:log:remove'))])
async def delete_contras_log_info(ids: str, query_db: AsyncSession = Depends(get_db)):
    delete_contras_log = DeleteContrastLogModel(ids=ids)
    delete_contras_log_result = await ContrastLogService.delete_operation_log_services(
        query_db, delete_contras_log
    )
    logger.info(delete_contras_log_result.message)

    return ResponseUtil.success(msg=delete_contras_log_result.message)


@contrasLogController.post('/export', dependencies=[Depends(CheckUserInterfaceAuth('contras:log:export'))])
async def export_contras_log_list(
        request: Request,
        contras_log_page_query: ContrastLogPageQueryModel = Depends(ContrastLogPageQueryModel.as_form),
        query_db: AsyncSession = Depends(get_db),
):
    # 获取全量数据
    contras_log_query_result = await ContrastLogService.get_contrast_log_list_services(
        query_db, contras_log_page_query, is_page=False
    )
    contras_log_export_result = await ContrastLogService.export_contrast_log_list_services(
        request, contras_log_query_result
    )
    logger.info('导出成功')

    return ResponseUtil.streaming(data=bytes2file_response(contras_log_export_result))


@contrasLogController.post('/test')
async def test(
        request: Request,
        contras_log: ContrastLogModel = Depends(ContrastLogModel),
        query_db: AsyncSession = Depends(get_db),
        current_user: CurrentUserModel = Depends(LoginService.get_current_user)
):
    contras_log.operator = current_user.user.user_name
    contras_log.source_file_name = '源文件名称'
    contras_log.source_file_path = '源文件地址'
    contras_log.target_file_name = '目标文件地址'
    contras_log.target_file_path = '目标文件地址'
    operation_log_result = await ComparisonLogUtil.insert_contrast_log(request, query_db, contras_log)
    print(operation_log_result)
    return ResponseUtil.success(msg=operation_log_result.message)
